<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Services\Articel as sArticel;
use App\Models\Articel   as mArticel;
use App\Services\Categories as sCategories;
use Illuminate\Http\Request;

use DB;
class ArticelController extends BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$articels = sArticel::getArticels();
		
		return $this->view('articel.index',compact('articels'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$categories = sCategories::getCategoriesList();

		return $this->view('articel.create',compact('categories'));
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(Request $request)
	{
		try{
			$data = $request->all();
			
			$data['add_time'] = time();
			
			mArticel::create($data);

			DB::commit();

			return $this->redirect('articel.index');

		}catch(\Exception $e){

			DB::rollback();
			
			return $this->redirect('articel.index');
		}

	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$articel = sArticel::getArticelDetail($id);

		if($articel->is_recommend == 0){
			$articel->is_recommend = 1;
		}else{
			$articel->is_recommend = 0;
		}

		$articel->save();

		return $this->redirect('articel.index');
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$categories = sCategories::getCategoriesList();
			
		$articel = sArticel::getArticelDetail($id);

		return $this->view('articel.update',compact('articel','categories'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update(Request $request,$id)
	{
		$data = $request->all(); 

		$articel = sArticel::getArticelDetail($id);

		$articel->update($data);

		return $this->redirect('articel.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
